package com.wedo.mall.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.listener.EListener;
import com.e.library.utils.EViewUtils;
import com.e.library.widget.ETitleBar;
import com.wedo.mall.R;
import com.wedo.mall.adapter.AgentCategoryAdapter;
import com.wedo.mall.entity.AgentCategory;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 注册-商家分类
 * Created by lxy on 2017/6/15.
 */

public class AgentCategoryActivity extends EActivity {

    @BindView(R.id.title_bar) ETitleBar title_bar;
    @BindView(R.id.rv_category) RecyclerView rv_category;

    private List<AgentCategory> selected = new ArrayList<>(3);

    @Override
    protected int layout() {
        return R.layout.activity_agent_category;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        rv_category.setHasFixedSize(true);
        EViewUtils.vertical(rv_category);

        categories();

        title_bar.setOnForwardListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selected.isEmpty()){
                    toast(R.string.register_category_select);
                    return;
                }

                if (selected.size() >3){
                    toast(R.string.register_category_tips);
                    return;
                }

                StringBuilder ids = new StringBuilder();
                StringBuilder names = new StringBuilder();
                for (int i = 0;i < selected.size();i ++){
                    ids.append(i ==0?"":",");
                    names.append(i== 0?"":",");
                    ids.append(selected.get(i).id);
                    names.append(selected.get(i).category_name);
                }

                Intent intent = new Intent();
                intent.putExtra("ids",ids.toString());
                intent.putExtra("names",names.toString());
                setResult(RESULT_OK,intent);
                defaultFinish();

            }
        });

    }

    private void categories(){
        loading();
        post(EApi.AGENT_CATEGORY, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                List<AgentCategory> list = response.list(AgentCategory.class);
                init(list);
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    private void init(List<AgentCategory> categories){
        final AgentCategoryAdapter adapter = new AgentCategoryAdapter(this,categories);
        rv_category.setAdapter(adapter);
        adapter.setItemClickListener(new EListener<AgentCategory>() {
            @Override
            public void onInvoked(View view, AgentCategory data, int position) {
                boolean checked = data.checked;
                if (checked){
                    selected.remove(data);
                }else {
                    selected.add(data);
                }

                data.checked = !checked;
                adapter.notifyItemChanged(position);
            }
        });
    }





}
